package com.qinglei.recoup.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qinglei.recoup.common.domain.CommonRes;
import com.qinglei.recoup.common.tenant.TenantContextHolder;
import com.qinglei.recoup.common.utils.RecoupUtil;
import com.qinglei.recoup.system.dao.BuildingMapper;
import com.qinglei.recoup.system.domain.Building;
import com.qinglei.recoup.system.domain.DeanBed;
import com.qinglei.recoup.system.domain.User;
import com.qinglei.recoup.system.pojo.BuildingShowVO;
import com.qinglei.recoup.system.pojo.PageParam;
import com.qinglei.recoup.system.service.BuildingService;
import com.qinglei.recoup.system.service.DeanBedService;
import com.qinglei.recoup.system.service.MonitorTimeService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author syp
 */
@Service
@AllArgsConstructor
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> implements BuildingService {

    private BuildingMapper buildingMapper;

//    @Autowired
//    private DeanBedService deanBedService;

    @Override
    public CommonRes<IPage<BuildingShowVO>> showPageBuilding(PageParam pageParam) {
        Page<BuildingShowVO> page = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());
        Integer tenantId = TenantContextHolder.getTenantId();
        IPage<BuildingShowVO> buildingShowVOIPage = buildingMapper.showPageBuilding(page, tenantId);
        return CommonRes.ok(buildingShowVOIPage);
    }

    @Override
    public CommonRes addBedCnt(Integer id) {
        Building building = getBaseMapper().selectById(id);
        building.setBedCnt(building.getBedCnt() + 1);
        int count = getBaseMapper().updateById(building);
        if (count > 0) {
            return CommonRes.ok();
        }
        throw new RuntimeException("添加楼床位数量失败");
    }

    @Override
    public CommonRes subBedCnt(Integer id, Integer cnt) {
        Building building = getBaseMapper().selectById(id);
        if (building.getBedCnt() > cnt) {
            building.setBedCnt(building.getBedCnt() - cnt);
        } else {
            building.setBedCnt(0);
        }
        int count = getBaseMapper().updateById(building);
        if (count > 0) {
            return CommonRes.ok();
        }
        throw new RuntimeException("减少楼床位数量失败");
    }

    /**
     * 修改监测时间是否统一
     *
     * @param id
     * @return
     */
    @Override
    public Boolean updateMonitorTimeUnified(Integer id) {
        try {
            Building building = new Building();
            building.setId(id);
            building.setMonitorTimeUnified(MonitorTimeService.UNIFIED_YES);
            baseMapper.updateById(building);
        } catch (Exception e) {
            throw new RuntimeException("操作失败");
        }
        return true;
    }

    /**
     * 修改离床阀值时间统一
     *
     * @param objId
     * @param outBedTimeout
     * @return
     */
    @Override
    public boolean updateOutBedTime(Integer objId, String outBedTimeout) {
        Building building = new Building();
        building.setId(objId);
        building.setOutBedTimeout(outBedTimeout);
        building.setOutBedTimeoutUnified(MonitorTimeService.UNIFIED_YES);
        int count = baseMapper.updateById(building);
        if (count > 0) {
            return true;
        }
        return false;
    }

    /**
     * 修改离床阀值时间不统一
     *
     * @param buildingId
     * @return
     */
    @Override
    public boolean updateOutBedTimeNo(Integer buildingId) {
        Building building = new Building();
        building.setId(buildingId);
        building.setOutBedTimeoutUnified(MonitorTimeService.UNIFIED_NO);
        int count = baseMapper.updateById(building);
        if (count > 0) {
            return true;
        }
        return false;
    }

    /**
     * 修改监控时间不统一
     *
     * @param buildingId
     * @return
     */
    @Override
    public boolean updateMonitorTimeNo(Integer buildingId) {
        Building building = new Building();
        building.setId(buildingId);
        building.setMonitorTimeUnified(MonitorTimeService.UNIFIED_NO);
        int count = baseMapper.updateById(building);
        if (count > 0) {
            return true;
        }
        return false;
    }

    @Override
    public String getNameWithParent(Integer buildingId) {
        String name = "";
        Building building = baseMapper.selectById(buildingId);
        if (building != null) {
            name = building.getName();
        }
        return name;
    }

    //
    @Override
    public List<Building> allDeanBuilding() {
//        User user = RecoupUtil.getCurrentUser();
//        QueryWrapper allQueryWrapper = new QueryWrapper();
//        allQueryWrapper.eq("user_id",user.getId());
//        allQueryWrapper.eq("type",5);
//        List<DeanBed> allBuildings = deanBedService.getBaseMapper().selectList(allQueryWrapper);
//        if(allBuildings!=null && allBuildings.size()>0){
//           return this.getBaseMapper().selectList(Wrappers.emptyWrapper());
//        }
//
//        QueryWrapper bedQueryWrapper = new QueryWrapper();
//        bedQueryWrapper.eq("user_id",user.getId());
//        bedQueryWrapper.eq("type",1);
//        List<DeanBed> beds = deanBedService.getBaseMapper().selectList(bedQueryWrapper);
//
//        QueryWrapper roomQueryWrapper = new QueryWrapper();
//        bedQueryWrapper.eq("user_id",user.getId());
//        bedQueryWrapper.eq("type",2);
//        List<DeanBed> rooms = deanBedService.getBaseMapper().selectList(roomQueryWrapper);
//
//
//        QueryWrapper floorQueryWrapper = new QueryWrapper();
//        bedQueryWrapper.eq("user_id",user.getId());
//        bedQueryWrapper.eq("type",3);
//        List<DeanBed> floors = deanBedService.getBaseMapper().selectList(floorQueryWrapper);
//
//
//        QueryWrapper buildingQueryWrapper = new QueryWrapper();
//        bedQueryWrapper.eq("user_id",user.getId());
//        bedQueryWrapper.eq("type",4);
//        List<DeanBed> buildings = deanBedService.getBaseMapper().selectList(bedQueryWrapper);



        return null;
    }


}
